//
//  LGShowPasswordVC.m
//  ALS3.0
//
//  Created by guomianxiang on 2017/4/19.
//  Copyright © 2017年 guomianxiang. All rights reserved.
//

#import "LGShowPasswordVC.h"
#import <AVFoundation/AVFoundation.h>
#import "ALSHeader.h"
@interface LGShowPasswordVC ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewLeft;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (assign, nonatomic) BOOL onceToken;
@property (strong, nonatomic) NSMutableArray <UIImageView *>*contentImageViewArr;

@end

@implementation LGShowPasswordVC


- (void)awakeFromNib {
    [super awakeFromNib];
    self.view.backgroundColor = [UIColor clearColor];
    self.contentViewLeft.constant = (kScreen_W - self.contentView.width) / 2;
    self.onceToken = YES;
}

- (void)viewDidLayoutSubviews {
    
    if (self.onceToken) {
        [self setup];
        self.onceToken = NO;
    }
}

- (void)setup {
    self.contentImageViewArr = [NSMutableArray array];
    for (int i=0; i<6; i++) {
        UIImageView *image = [[UIImageView alloc]init];
        [self.contentImageViewArr addObject:image];
    }
    
    
    for (int i=0; i<self.contentView.subviews.count; i++) {
        UIView *view = self.contentView.subviews[i];
        NSInteger index = view.x / 47;
        
        if ([view.subviews[0] isKindOfClass:[UIImageView class]]) {
//            NSLog(@"index--%li",index);
            self.contentImageViewArr[index] = view.subviews[0];
//            [self.contentImageViewArr insertObject:view.subviews[0] atIndex:index];
        }
    }
}

- (void)addCircle {
    for (NSInteger i=0; i<self.contentImageViewArr.count; i++) {
        UIImageView *imageView = self.contentImageViewArr[i];
        if (!imageView.highlighted) {
            imageView.highlighted = YES;
            break;
        }
    }
}

- (void)removeLastCircle {
    
    for (NSInteger i=self.contentImageViewArr.count - 1; i>=0; i--) {

        UIImageView *imageView = self.contentImageViewArr[i];
        if (imageView.highlighted) {
            imageView.highlighted = NO;
            break;
        }
    }
}

- (void)removeAllCirCle {
    for (NSInteger i=0; i<self.contentImageViewArr.count; i++) {
        UIImageView *imageView = self.contentImageViewArr[i];
        imageView.highlighted = NO;
    }

}

- (void)startErrorAnimalAndRemoveAllCircle {
    __weak typeof(self)weakSelf = self;
    
//    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    
    for (NSInteger i=0; i<weakSelf.contentImageViewArr.count; i++) {
        UIImageView *imageView = self.contentImageViewArr[i];
        imageView.highlighted = YES;
    }

    
    self.contentViewLeft.constant = (kScreen_W - self.contentView.width) / 2 + 20;
    [self.view layoutIfNeeded];
    
    self.contentViewLeft.constant = (kScreen_W - self.contentView.width) / 2;
    
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:100 options:(UIViewAnimationOptionLayoutSubviews) animations:^{
        
        [self.view layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
        for (NSInteger i=0; i<weakSelf.contentImageViewArr.count; i++) {
            UIImageView *imageView = weakSelf.contentImageViewArr[i];
            imageView.highlighted = NO;
        }
    }];

}

//- (void)addChildView {
//    self.contentImageViewArr = [NSMutableArray array];
//    
//    CGFloat Spacing = 3.0;
//    CGFloat width = 44.0;
//    CGFloat edge = (kScreen_W - width * 6 + 5 * Spacing) / 2;
//    
//    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_W, 44)];
//    
//    self.contentView = contentView;
//    self.contentView.backgroundColor = [UIColor clearColor];
//    
//    for (int i=0; i<6; i++) {
//        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(Spacing + (width + Spacing) * i, 0, width, width)];
//        view.backgroundColor = [UIColor clearColor];
//        UIImageView *background = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
//        [view addSubview:background];
//        UIImageView *content = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
//        [view addSubview:content];
//        [self.contentImageViewArr addObject:content];
//    }
//    
//}


- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
